#include "headers/SquaresList.h"

#include "headers/LevelMenager.h"
#include "headers/Windows.h"
#include "headers/RGB.h"
#include "headers/Fonts.h"

#include <sstream>

extern WindowsMenager cWindowsMenager;
extern LevelMenager cLevelMenager;

int HowMany(int lvl) {
    if (0<=lvl && lvl<=7) return 99;
    if (8<=lvl && lvl<=0xf) return 251;
    if (lvl==0x11) return 251;
    if (lvl==0x12) return 98;
    if (lvl==0x13) return 251;
    if (lvl==0x14) return 160;
    if (lvl==0x15 || lvl==0x16) return 131;
    if (lvl==0x17) return 134;
    if (lvl==0x18) return 185;
    if (0x19<=lvl && lvl<=0x1c) return 182;
    if (0x1d<=lvl && lvl<=0x1f) return 239;
    if (0x20<=lvl && lvl<=0x21) return 85;
    if (0x22<=lvl && lvl<=0x24) return 159;
    if (0x25<=lvl && lvl<=0x2b) return 26;
    if (lvl==0x2c) return 159;
    if (0x2d<=lvl && lvl<=0x2f) return 160;
    if (lvl==0x30) return 159;
    if (lvl==0x31) return 131;
    
    return 1;
}
       
SquaresList::SquaresList(int _w):
    w(_w), space(2) {
}

int SquaresList::DrawSquares(const char* WinName) {
    
    int id;
    
    int HM = HowMany(cLevelMenager.GetCurrentLvl());
    cWindowsMenager.Clear(WinName);
    cWindowsMenager.Resize(WinName, 338, (((HM/w)+1)*32) + ((HM/w)*space));
    for (int id=0, i=0, j=0; id<=HowMany(cLevelMenager.GetCurrentLvl()); id++) {
        cLevelMenager.DrawSquare(WinName, id, i*(32+space), j*(32+space));
        i++;
        if (i==w) {
            i=0;
            j++;
        }
    }
    
}

int SquaresList::DrawFrame(const char* WinName) {
    
    int SqrX = SqrCur%w;
    int SqrY = SqrCur/w;
        
    cWindowsMenager.DrawFrame(WinName, GetRGB(0, 200, 0), SqrX*(32+space), SqrY*(32+space), 32, 32);   

}

int SquaresList::MouseClick(int x, int y, int b) {
       
    x /= (32+space);
    y /= (32+space);
    
    if ( x<w ) {
        int id = x+y*w;
        if ( id<=HowMany(cLevelMenager.GetCurrentLvl()) )
            SetCurrentSquare(id);
    }
    
    cLevelMenager.SetCurrentSquareID(SqrCur);
    
}

int SquaresList::SetCurrentSquare(unsigned char id) {
    
    int SqrX = SqrCur%w;
    int SqrY = SqrCur/w;
    
    SqrCur = id;
    
} 
    
int SquaresList::SetW(int _w) {
    w = _w;
}
